package com.bite.springblog.common.interceptor;

import com.bite.springblog.common.exception.BlogException;
import com.bite.springblog.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        log.info("从header中获取token:"+token);
        if(token == null){
            response.setStatus(401);
            return false;
        }
        try {
            Claims claims = JwtUtils.cheakToken(token);
            if(claims != null){
                return true;
            }
            response.setStatus(401);
            return false;
        }catch (Exception e){

        }
        return true;
    }
}
